var tabName = "good", navName = "all", supplierState = null, orderNumber = $("span[prop='orderNumber']");
var goodContainer = $(".good-container"), adjustPriceDialog = $("#adjustPriceDialog");

$(function() {
	$(".weui-tabbar__item").click(function() {
		if (supplierState == null) {
			mobileGlobal.message("未能加载状态信息");
			return;
		}
		
		var $this = $(this), prop = $this.attr("prop");
		if (!supplierState && prop != "me") {
			mobileGlobal.message("注册信息未通过审核");
			return;
		}
		
		$this.addClass("weui-bar__item_on").siblings(".weui-bar__item_on").removeClass("weui-bar__item_on");
		$(".weui-tab__panel[prop='" + prop + "']").removeClass("hidden").siblings(".weui-tab__panel").addClass("hidden");
		
		switch (prop) {
			case "good":
				loadGood();
				break;
			case "order":
				loadOrder(navName);
				break;
			case "me":
				loadPersonalInfo();
				break;
			default:
				return;
		}
	});
	$(".weui-navbar__item").click(function() {
		if (!supplierState) return;
		
		loadOrder($(this).attr("prop"));
	});
	
	var pageParams = mobileGlobal.getPageParams(), tempTabName = null, tempNavName = null;
	if (pageParams) {
		pageParams = eval("(" + pageParams + ")");
		tempTabName = pageParams.tabName;
		tempNavName = pageParams.navName;
	}
	
	mobileGlobal.sendRequest(__mobilectx + "/supplier/checkSupplierState.xhtml", null, null, function(data, status) {
		mobileGlobal.hideLoading();
		
		if (!data.status) mobileGlobal.failed("加载数据失败");
		else {
			supplierState = data.result.supplierState;
			
			if (!data.result.supplierState) tabName = "me";
			else {
				if (tempTabName) tabName = tempTabName;
				else {
					if (data.result.orderNumber > 0) tabName = "order";
				}
				
				if (tabName === "order") {
					if (tempNavName) navName = tempNavName;
					else {
						if (data.result.orderNumber > 0) navName = "distributing";
					}
				}
				
				if (data.result.orderNumber > 0) orderNumber.text(data.result.orderNumber).removeClass("hidden");
			}
			
			$(".weui-tabbar__item[prop='" + tabName + "']").click();
		}
	});
});

//加载商品
function loadGood() {
	mobileGlobal.sendRequest(__mobilectx + "/supplier/good/loadMgrListData.xhtml", null, null, function(data, status) {
		if (data.status) {
			goodContainer.empty();
			
			for (var i = 0; i < data.result.length; i ++) {
				var good = data.result[i];
				var html = "<div class='good-item' prop='" + good.goodid + "'>" + 
							"<img src='" + good.goodpic + "'>" + 
							"<div class='text'>" + 
								"<div class='name'>【" + good.goodcode + "】" + good.goodname + "</div>" + 
								"<div class='specs'>" + good.goodspecs + "</div>" + 
								"<div class='price'>￥<span class='number'>" + good.goodprice.toFixed(2) + "</span>元/<span>" + good.meteringunit + "</span></div>" + 
								"<div class='manufacturer'>" + good.manufacturer + "</div>" + 
								"<div class='operate'>" + 
									"<i class='fa fa-cny' onclick='adjustPrice(\"" + good.goodid + "\", " + good.goodprice + ");'></i>" + 
									"<i class='fa fa-edit' onclick='editGood(\"" + good.goodid + "\");'></i>" + 
									"<i class='fa fa-trash' onclick='deleteGood(\"" + good.goodid + "\");'></i>" + 
								"</div>" + 
							"</div>" + 
						   "</div>";
				goodContainer.append($(html));
			}
			
			mobileGlobal.hideLoading();
		} else {
			mobileGlobal.hideLoading();
			mobileGlobal.failed("加载数据失败");
		}
	});
}

//商品调价
function adjustPrice(goodid, goodprice) {
	adjustPriceDialog.find("input[type='hidden']").val(goodid);
	adjustPriceDialog.find("input[type='text']").val(goodprice);
	
	adjustPriceDialog.fadeIn(200);
}

//确认调价
function confirmAdjustPrice() {
	var goodid = adjustPriceDialog.find("input[type='hidden']").val();
	if (!goodid) {
		mobileGlobal.message("未找到商品");
		return;
	}
	var goodprice = adjustPriceDialog.find("input[type='text']").val();
	if (goodprice) {
		if (!/^(0|([1-9]\d*))(\.\d{1,2})?$/.test(goodprice)) {
			mobileGlobal.message("商品价格必须为有效的数字");
			return;
		}
	} else goodprice = "0";
	
	closeAdjustPrice();
	
	mobileGlobal.sendRequest(__mobilectx + "/supplier/good/adjustPrice.xhtml", {goodid : goodid, goodprice : goodprice}, null, 
		function(data, status) {
			mobileGlobal.hideLoading();
			if (data.status) {
				mobileGlobal.openMessageDialog("商品调价成功！", function() {
					mobileGlobal.closeMessageDialog();
					$(".good-item[prop='" + goodid + "']").find("span.number").text(parseFloat(goodprice).toFixed(2));
				});
			} else {
				mobileGlobal.openMessageDialog(data.result, function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		}, 
		function(xhr, msg, e) {
			mobileGlobal.hideLoading();
			mobileGlobal.openMessageDialog("网络请求错误！", function() {
				mobileGlobal.closeMessageDialog();
			});
		}
	);
}

//关闭调价
function closeAdjustPrice() {
	adjustPriceDialog.find("input[type='hidden']").val("");
	adjustPriceDialog.find("input[type='text']").val("");
	
	adjustPriceDialog.fadeOut(200);
}

//编辑商品
function editGood(goodid) {
	if (!supplierState) return;
	
	var param = (goodid) ? "?goodid=" + goodid : "";
	mobileGlobal.forward(__mobilectx + "/supplier/good/edit.xhtml" + param, true, "{'tabName' : 'good'}");
}

//删除商品
function deleteGood(goodid) {
	mobileGlobal.openConfirmDialog("确定要删除该商品？", function() {
		mobileGlobal.closeConfirmDialog();
		
		mobileGlobal.sendRequest(__mobilectx + "/supplier/good/deleteGood.xhtml", {goodid : goodid}, null, 
			function(data, status) {
				mobileGlobal.hideLoading();
				if (data.status) {
					mobileGlobal.openMessageDialog("删除商品成功！", function() {
						mobileGlobal.closeMessageDialog();
						$(".good-item[prop='" + goodid + "']").remove();
					});
				} else {
					mobileGlobal.openMessageDialog(data.result, function() {
						mobileGlobal.closeMessageDialog();
					});
				}
			}, 
			function(xhr, msg, e) {
				mobileGlobal.hideLoading();
				mobileGlobal.openMessageDialog("网络请求错误！", function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		);
	});
}

//加载订单
function loadOrder(prop) {
	var params = null;
	switch (prop) {
		case "all":
			params = {state : null};
			break;
		case "distributing":
			params = {state : sysGlobal.EntityState[7].value};
			break;
		case "completed":
			params = {state : sysGlobal.EntityState[9].value};
			break;
		case "cancelled":
			params = {state : sysGlobal.EntityState[10].value};
			break;
		default:
			return;
	}
	
	var $this = $("div.weui-navbar__item[prop='" + prop + "']");
	var orderContainer = $this.parent().siblings(".weui-tab__panel");
	
	if (!$this.hasClass("weui-bar__item_on")) {
		$this.addClass("weui-bar__item_on").siblings(".weui-bar__item_on").removeClass("weui-bar__item_on");
	}
	
	mobileGlobal.sendRequest(__mobilectx + "/supplier/order/loadOrderData.xhtml", params, null, function(data, status) {
		if (data.status) {
			orderContainer.empty();
			
			for (var i = 0; i < data.result.orders.length; i ++) {
				var order = data.result.orders[i];
				
				var ogHtml = "";
				for (var j = 0; j < order.ordergoods.length; j ++) {
					ogHtml += "<img src='" + order.ordergoods[j].goodpic + "'/>";
				}
				
				var btnHtml = "";
				switch (order.orderstate) {
					case 8:
						btnHtml = "<span class='button' prop='distribute'>确认发货</span>";
						break;
					case 9:
						btnHtml = "<span class='button' prop='cancel'>取消订单</span>";
						break;
				}
				
				var oHtml = "<div class='order-item' prop='" + order.orderid + "'>" + 
								"<div class='top'>" + 
									"<div class='name'><i class='fa fa-vcard'></i>" + order.entname + "</div>" + 
									"<div class='state'>" + sysGlobal.getEntityStateText(order.orderstate) + "</div>" + 
								"</div>" + 
								"<div class='middle'>" + 
									"<div class='left'>" + ogHtml + "</div>" + 
									"<div class='right'>" + 
										"<div class='total'>￥<span class='number'>" + order.ordertotal.toFixed(2) + "</span>元</div>" + 
										"<div class='count'>共" + order.ordergoods.length + "件</div>" + 
									"</div>" + 
								"</div>" + 
								"<div class='bottom'>" + btnHtml + "</div>" + 
							"</div>";
				var orderItem = $(oHtml);
				orderItem.click(viewOrderDetail);
				orderItem.find("span.button[prop='distribute']").click(confirmDistribute);
				orderItem.find("span.button[prop='cancel']").click(cancelOrder);
				
				orderContainer.append(orderItem);
			}
			
			if (data.result.handlingNumber > 0) {
				orderNumber.text(data.result.handlingNumber).removeClass("hidden");
			} else {
				orderNumber.text(0).addClass("hidden");
			}
			
			mobileGlobal.hideLoading();
		} else {
			mobileGlobal.hideLoading();
			mobileGlobal.failed("加载数据失败");
		}
	});
}

//查看订单详情
function viewOrderDetail() {
	var orderid = $(this).attr("prop"), navname = $("div.weui-navbar__item.weui-bar__item_on").attr("prop");
	mobileGlobal.forward(__mobilectx + "/supplier/order/detail.xhtml?orderid=" + orderid, true, "{'tabName' : 'order', 'navName' : '" + navname + "'}");
}

//确认发货
function confirmDistribute() {
	var e = window.event || arguments.callee.caller.arguments[0];
	if (e.stopPropagation) {
		e.stopPropagation();
	} else e.cancelBubble = true;
	
	var $this = $(this), orderid = $this.parent().parent().attr("prop");
	
	mobileGlobal.openConfirmDialog("订单确定已发货？", function() {
		mobileGlobal.closeConfirmDialog();
		
		mobileGlobal.sendRequest(__mobilectx + "/supplier/order/confirmDistribute.xhtml", {orderid : orderid}, null, 
			function(data, status) {
				mobileGlobal.hideLoading();
				if (data.status) {
					mobileGlobal.openMessageDialog("确认发货成功！", function() {
						mobileGlobal.closeMessageDialog();
						$("div.weui-navbar__item.weui-bar__item_on").click();
					});
				} else {
					mobileGlobal.openMessageDialog(data.result, function() {
						mobileGlobal.closeMessageDialog();
					});
				}
			}, 
			function(xhr, msg, e) {
				mobileGlobal.hideLoading();
				mobileGlobal.openMessageDialog("网络请求错误！", function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		);
	});
}

//取消订单
function cancelOrder() {
	var e = window.event || arguments.callee.caller.arguments[0];
	if (e.stopPropagation) {
		e.stopPropagation();
	} else e.cancelBubble = true;
	
	var $this = $(this), orderid = $this.parent().parent().attr("prop");
	
	mobileGlobal.openConfirmDialog("确定取消订单？", function() {
		mobileGlobal.closeConfirmDialog();
		
		mobileGlobal.sendRequest(__mobilectx + "/supplier/order/cancelOrder.xhtml", {orderid : orderid}, null, 
			function(data, status) {
				mobileGlobal.hideLoading();
				if (data.status) {
					mobileGlobal.openMessageDialog("取消订单成功！", function() {
						mobileGlobal.closeMessageDialog();
						$("div.weui-navbar__item.weui-bar__item_on").click();
					});
				} else {
					mobileGlobal.openMessageDialog(data.result, function() {
						mobileGlobal.closeMessageDialog();
					});
				}
			}, 
			function(xhr, msg, e) {
				mobileGlobal.hideLoading();
				mobileGlobal.openMessageDialog("网络请求错误！", function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		);
	});
}

//加载个人信息
function loadPersonalInfo() {
	mobileGlobal.sendRequest(__mobilectx + "/supplier/loadPersonalInfo.xhtml", null, null, function(data, status) {
		if (data.status) {
			var personalInfo = $(".personal-info");
			personalInfo.empty();
			
			switch (data.result.supplier.entitystate) {
			case 4:
				personalInfo.append($("<span>资料审核未通过，点击&nbsp;<a href='javascript:void(0);' onclick='resubmit();'>重新提交</a>&nbsp;资料！</span>"));
				break;
			case 5:
				personalInfo.append($("<span>资料审核中，请耐心等待！</span>"));
				break;
			case 6:
				personalInfo.append($("<span>资料已审核通过，更多功能敬请期待！</span>"));
				break;
			}
			
			mobileGlobal.hideLoading();
		} else {
			mobileGlobal.hideLoading();
			mobileGlobal.failed("加载数据失败");
		}
	});
}

//重新提交
function resubmit() {
	mobileGlobal.forward(__mobilectx + "/supplier/resubmit.xhtml", true, "{'tabName' : 'me'}");
}
